package com.ruyuan.commodity.controller.buyer;

import com.ruyuan.commodity.common.mamager.BrandManager;
import com.ruyuan.commodity.common.mamager.CategoryManager;
import com.ruyuan.commodity.model.goods.vo.BrandVO;
import com.ruyuan.commodity.model.goods.vo.CategoryVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 商品分类控制器
 */
@RestController
@RequestMapping("/goods/categories")
public class CategoryBuyerController {

    @Autowired
    private CategoryManager categoryManager;

    @Autowired
    private BrandManager brandManager;

    @ApiOperation(value = "首页等商品分类数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parent_id", value = "分类id，顶级为0", required = true, dataType = "int", paramType = "path"),})
    @GetMapping(value = "/{parent_id}/children")
    public List<CategoryVO> list(@PathVariable("parent_id") Long parentId) {

        List<CategoryVO> catTree = categoryManager.listAllChildren(parentId);

        for (CategoryVO cat : catTree) {
            // 一级分类显示关联的品牌
            if (cat.getParentId().compareTo(parentId) == 0) {
                List<BrandVO> brandList = brandManager.getBrandsByCategory(cat.getCategoryId());
                cat.setBrandList(brandList);
            }
        }

        return catTree;
    }
}
